import { createRouter, createWebHistory } from 'vue-router'
import store from '@/store'

const routes = [
  {
    path: '/foundMusic',
    name: 'FoundMusic',
    component: () => import('../views/FoundMusic.vue'),
    //进入之前 对头部导航进行 显示
    beforeEnter: (to, from, next) => {
      if (to.path == '/foundMusic') {
        store.state.isNav = true
        next()
      } else {
        next()
      }
    }
  },
  {
    path: '/itemMusic',
    name: 'ItemMusic',
    component: () => import('../views/ItemMusic.vue'),
    //进入之前 对头部导航进行 隐藏
    beforeEnter: (to, from, next) => {
      if (to.path == '/itemMusic') {
        store.state.isNav = false
        next()
      } else {
        next()
      }
    }
  },
  {
    path: '/search',
    name: 'Search',
    component: () => import('../views/Search.vue'),
    //进入搜索页面对头部导航进行隐藏
    beforeEnter: (to, from, next) => {
      if (to.path == '/search') {
        store.state.isNav = false
        next()
      } else {
        next()
      }
    }
  },
  {
    path: '/userLogin',
    name: 'UserLogin',
    component: () => import('../views/UserLogin.vue'),
    beforeEnter: (to, from, next) => {
      if (to.path == '/userLogin') {
        store.state.isNav = true
        next()
      } else {
        next()
      }
    }
  },
  {
    path: '/userCenter',
    name: 'UserCenter',
    component: () => import('../views/UserCenter.vue'),
    //对用户是否登录进行限制
    beforeEnter: (to, from, next) => {
      if (to.path == '/userCenter' && store.state.user.isLogin || store.state.user.token || sessionStorage.getItem('token')) {
        store.state.isNav = true
        next()
      } else {
        next('/userLogin')
      }
    }
  },
  //重定向
  {
    path: '/',
    redirect: '/foundMusic'
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})


export default router
